using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Finance.DTO.FinanceDetail;
using AnQinFrameWorkAPI.Entity.Finance.Entity;
using AnQinFrameWorkAPI.Entity.Finance.View.FinanceDetail;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Finance;

/// <summary>
///     财务明细业务层
/// </summary>
public class FinanceDetailService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public FinanceDetailService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取财务明细列表

    public async Task<RequestResponseModel> GetFinanceDetailList(FinanceDetailSearchDTO financeDetailSearchDTO)
    {
        //筛选
        var query = _context.FinanceDetail
                .ProjectTo<FinanceDetailView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(a => a.FinanceCategoryId.ToString() == financeDetailSearchDTO.FinanceCategoryId, 
                    !string.IsNullOrEmpty(financeDetailSearchDTO.FinanceCategoryId))
                .WhereIF(a => a.FinanceAccountId.ToString() == financeDetailSearchDTO.FinanceAccountId, 
                    !string.IsNullOrEmpty(financeDetailSearchDTO.FinanceAccountId))
                .WhereIF(a => a.Name.Contains(financeDetailSearchDTO.Name),
                    !string.IsNullOrEmpty(financeDetailSearchDTO.Name))
                .WhereIF(a => (int)a.Type == financeDetailSearchDTO.Type, financeDetailSearchDTO.Type != null)
                .WhereIF(a => a.TransactionTime > financeDetailSearchDTO.StartTime, financeDetailSearchDTO.StartTime != null)
                .WhereIF(a => a.TransactionTime < financeDetailSearchDTO.EndTime, financeDetailSearchDTO.EndTime != null)
            ;

        //返回列表
        var financeDetailList = await query
            .OrderBy(a => a.TransactionTime)
            .Page(financeDetailSearchDTO.PageIndex, financeDetailSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<FinanceDetailView>(financeDetailList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增财务明细

    public async Task<RequestResponseModel> AddFinanceDetail(FinanceDetailAddDTO financeDetailAddDTO)
    {
        //生成新数据
        var financeDetail = _mapper.Map<FinanceDetailModel>(financeDetailAddDTO);

        financeDetail.Id = Guid.NewGuid();
        financeDetail.UserId = Guid.NewGuid(); // 需要从上下文获取当前用户ID
        financeDetail.CreateTime = DateTime.Now;

        //新增数据
        _context.FinanceDetail.Add(financeDetail);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeDetail.Id, "新增成功");
    }

    #endregion

    #region 更新财务明细

    public async Task<RequestResponseModel> PutFinanceDetail(FinanceDetailUpdateDTO financeDetailUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceDetail, financeDetailUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务明细Id无效");

        //编辑数据
        var financeDetail = _mapper.Map(financeDetailUpdateDTO, query);

        //更新表
        _context.FinanceDetail.Update(financeDetail);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeDetailUpdateDTO.Id);
    }

    #endregion

    #region 删除财务明细

    public async Task<RequestResponseModel> DeleteFinanceDetail(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceDetail, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务明细Id无效");

        //删除数据
        _context.FinanceDetail.Remove(query);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion
}